package com.lntea.mybatisDemo.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import com.lntea.mybatisDemo.domain.CmCorporation;
import com.lntea.mybatisDemo.domain.TransInfoBean;
import com.lntea.mybatisDemo.mapper.TransInfoBeanMapper;

public class TransInfoBeanService {
	private SqlSessionFactory sqlSessionFactory;

	@Before
	public void beforeQuery() {
		String resource = "mybatis-config.xml";
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	//查询商户当日交易是否有指定金额的结算
	public void queryTransInfoCountCurrent(){
		SqlSession session = sqlSessionFactory.openSession();
		try{
			TransInfoBeanMapper mapper = session.getMapper(TransInfoBeanMapper.class);
			CmCorporation cmCorporation = new CmCorporation();
			cmCorporation.setMerchantNo("102002858120033");
			cmCorporation.setThanValue("10000");
			Integer result = mapper.queryTransInfoCountCurrent(cmCorporation);
			System.out.println(result);
		} finally {
			session.close();
		}
	}
	
	@Test
	//查询商户历史交易是否有指定金额的结算
	public void queryTransInfoCountHistory(){
		SqlSession session = sqlSessionFactory.openSession();
		try{
			TransInfoBeanMapper mapper = session.getMapper(TransInfoBeanMapper.class);
			CmCorporation cmCorporation = new CmCorporation();
			cmCorporation.setMerchantNo("Z11001058121417");
			cmCorporation.setThanValue("10000");
			Integer result = mapper.queryTransInfoCountHistory(cmCorporation);
			System.out.println(result);
		} finally {
			session.close();
		}
	}
	
	@Test
	//查询交易记录数
	public void queryTransCount(){
		SqlSession session = sqlSessionFactory.openSession();
		try{
			TransInfoBeanMapper mapper = session.getMapper(TransInfoBeanMapper.class);
			TransInfoBean transInfoBean = new TransInfoBean();
			transInfoBean.setStartDate("2015-01-01");
			transInfoBean.setEndDate("2015-08-10");
			transInfoBean.setMerchantNo("102002858120033");
			Integer count = mapper.queryTransCount(transInfoBean);
			System.out.println(count);
		} finally {
			session.close();
		}
	}
	
	@Test
	//查询交易列表
	public void queryTransList(){
		SqlSession session = sqlSessionFactory.openSession();
		try{
			TransInfoBeanMapper mapper = session.getMapper(TransInfoBeanMapper.class);
			TransInfoBean transInfoBean = new TransInfoBean();
			transInfoBean.setStartDate("2015-01-01");
			transInfoBean.setEndDate("2015-08-10");
			transInfoBean.setMerchantNo("102002858120033");
			transInfoBean.setPage(3);
			transInfoBean.setRows(10);
			Integer count = mapper.queryTransCount(transInfoBean);
			if(count > 0){
				transInfoBean.getPager().setRowCount(count);
				List<TransInfoBean> list = mapper.queryTransList(transInfoBean);
				for(TransInfoBean temp : list){
					System.out.println(temp.getTransDateTime());
				}
			}
			System.out.println(count);
		} finally {
			session.close();
		}
	}
	
	@Test
	//查询交易详细信息
	public void queryTransDetail(){
		SqlSession session = sqlSessionFactory.openSession();
		try{
			TransInfoBeanMapper mapper = session.getMapper(TransInfoBeanMapper.class);
			TransInfoBean transInfoBean = new TransInfoBean();
			transInfoBean.setMerchantNo("102002858120033");
			transInfoBean.setId(29202);
			TransInfoBean result = mapper.queryTransDetail(transInfoBean);
			System.out.println(result.getTraceNo());
		} finally {
			session.close();
		}
	}
}
